/*
 * @Author: lineCode 1145938682@qq.com
 * @Date: 2025-01-01 17:42:52
 * @LastEditors: lineCode 1145938682@qq.com
 * @LastEditTime: 2025-02-19 13:29:23
 * @FilePath: /backend/shorthand-modules/shorthand-modules-content/src/main/java/com/angus/modules/content/repository/SysUnitRepository.java
 */
package com.angus.modules.content.repository;

import com.angus.common.security.utils.SecurityUtils;
import com.angus.modules.content.domain.SysUnit;
import com.angus.modules.content.mapper.SysUnitMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author liyanan
 * @date 2024/10/21 3:07 PM
 * @description
 */
@Repository
public class SysUnitRepository extends ServiceImpl<SysUnitMapper, SysUnit> {
    public Integer selectMaxUnitNum(String courseCode) {
       return baseMapper.selectUnitNum(courseCode);
    }

    public Long selectGroupCountByUnitNum(Integer unitNum, String courseCode) {
       return baseMapper.selectCount(Wrappers.lambdaQuery(SysUnit.class)
                .eq(SysUnit::getUnitNum, unitNum)
                .eq(SysUnit::getCourseCode, courseCode)
                .eq(SysUnit::getDelFlag, 0));
    }

    public List<SysUnit> selectAllUnits(String courseCode) {
       return baseMapper.selectList(new QueryWrapper<SysUnit>()
           .select(
               "MAX(id) AS id",
               "unit_num",  // 非聚合字段需在 GROUP BY 中
               "group_id",  // 非聚合字段需在 GROUP BY 中
               "course_code",
               "MAX(is_pay) AS is_pay",
               "MAX(create_time) AS create_time",
               "MAX(update_time) AS update_time",
               "MAX(create_by) AS create_by",
               "MAX(update_by) AS update_by",
               "MAX(remark) AS remark"
           ).eq("course_code", courseCode)
            .eq("del_flag", 0)
           .groupBy("unit_num, group_id")
           .orderByAsc(Arrays.asList("unit_num", "group_id")));
    }

    public List<SysUnit> selectAllUnits(String courseCode,Long groupId) {
        return baseMapper.selectList(new QueryWrapper<SysUnit>()
            .select(
                "MAX(id) AS id",
                "unit_num",  // 非聚合字段需在 GROUP BY 中
                "group_id",  // 非聚合字段需在 GROUP BY 中
                "course_code",
                "MAX(is_pay) AS is_pay",
                "MAX(create_time) AS create_time",
                "MAX(update_time) AS update_time",
                "MAX(create_by) AS create_by",
                "MAX(update_by) AS update_by",
                "MAX(remark) AS remark"
            ).eq("course_code", courseCode)
            .eq("group_id", groupId)
            .eq("del_flag", 0)
            .groupBy("unit_num, group_id")
            .orderByAsc(Arrays.asList("unit_num", "group_id")));
    }


    public List<SysUnit> selectUnits(String courseCode, Integer unitNum) {
        return baseMapper.selectList(new QueryWrapper<SysUnit>()
            .select(
                "MAX(id) AS id",
                "unit_num",  // 非聚合字段需在 GROUP BY 中
                "group_id",  // 非聚合字段需在 GROUP BY 中
                "course_code",
                "MAX(is_pay) AS is_pay",
                "MAX(create_time) AS create_time",
                "MAX(update_time) AS update_time",
                "MAX(create_by) AS create_by",
                "MAX(update_by) AS update_by",
                "MAX(remark) AS remark"
            ).eq("course_code", courseCode)
            .eq("unit_num", unitNum)
            .eq("del_flag", 0)
            .groupBy("unit_num, group_id")
            .orderByAsc(Arrays.asList("unit_num", "group_id")));
    }

    public List<SysUnit> selectGroupID(String courseCode,Integer unitNum,Long groupId) {
        return baseMapper.selectList(new QueryWrapper<SysUnit>()
            .select(
                "MAX(id) AS id",
                "unit_num",  // 非聚合字段需在 GROUP BY 中
                "group_id",  // 非聚合字段需在 GROUP BY 中
                "course_code",
                "MAX(is_pay) AS is_pay",
                "MAX(create_time) AS create_time",
                "MAX(update_time) AS update_time",
                "MAX(create_by) AS create_by",
                "MAX(update_by) AS update_by",
                "MAX(remark) AS remark"
            ).eq("course_code", courseCode)
            .eq("unit_num", unitNum)
            .eq("group_id", groupId)
            .eq("del_flag", 0)
            .groupBy("unit_num, group_id")
            .orderByAsc(Arrays.asList("unit_num", "group_id")));
    }

    public boolean setContentFee(List<Integer> unitNums,
                              String courseCode, Integer isPay) {
        String username = SecurityUtils.getUsername();
        int update = baseMapper.update(null, Wrappers.lambdaUpdate(SysUnit.class)
                .set(SysUnit::getIsPay, isPay)
                .set(SysUnit::getUpdateBy,username)
                .set(SysUnit::getUpdateTime, Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()))
                .eq(SysUnit::getCourseCode, courseCode)
                .eq(SysUnit::getDelFlag, 0)
                .in(SysUnit::getUnitNum, unitNums));
        return update > 0;
    }
}
